package com.hnkywl.sts.service.taskdeal;

import com.hnkywl.sts.dao.organ.NbgsDao;
import com.hnkywl.sts.dao.sys.DsgdDao;
import com.hnkywl.sts.dao.xsgl.KhDao;
import com.hnkywl.sts.entity.organ.Dsgd;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.service.kcgl.KcgdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jerry on 14-2-7.
 */
@Service
public class DoKhGdqcOrderTimeSync {
    private KhDao khDao;
    private NbgsDao nbgsDao;
    private KcgdService kcgdService;
    private DsgdDao dsgdDao;


    @Transactional(readOnly = true)
    public List<Kh> findAllGdKhToJy(Nbgs nbgs) {
        String hql = "from Kh where (gdsl is null or gdsl = 0) and kkdsl<>0 and qk=? and cggs.id=? order by id desc";
        List<Kh> khList = this.khDao.find(hql, false, nbgs.getId());
        return khList == null ? new ArrayList<Kh>() : khList;
    }


    @Transactional
    public void doGdqcSyncForOrderTime() {
        List<Dsgd> dsgdList = this.dsgdDao.findAll();
        if (dsgdList != null && dsgdList.size() > 0) {
            for (Dsgd dsgd : dsgdList) {
                List<Kh> khs = this.findAllGdKhToJy((Nbgs) dsgd.getNbgs());
                try {
                    kcgdService.gdToOrderTime("all", khs);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Autowired
    public void setKhDao(KhDao khDao) {
        this.khDao = khDao;
    }

    @Autowired
    public void setNbgsDao(NbgsDao nbgsDao) {
        this.nbgsDao = nbgsDao;
    }

    @Autowired
    public void setKcgdService(KcgdService kcgdService) {
        this.kcgdService = kcgdService;
    }

    @Autowired
    public void setDsgdDao(DsgdDao dsgdDao) {
        this.dsgdDao = dsgdDao;
    }
}
